package com.zuipin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuipin.entity.OrderInfo;
import com.zuipin.entity.OrderInvoiceInfo;
import com.zuipin.enums.InvoiceTitle;
import com.zuipin.enums.InvoiceType;
import com.zuipin.enums.OrderStatusEnum;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.OrderInvoiceInfoMapper;
import com.zuipin.service.IOrderInvoiceInfoService;
import com.zuipin.service.IOrderService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.OrderInvoiceInfoVo;
import com.zuipin.vo.StatusNumVo;

@Service
public class OrderInvoiceInfoServiceImpl implements IOrderInvoiceInfoService {
	
	@Resource
	private OrderInvoiceInfoMapper	orderInvoiceInfoMapper;
	@Resource
	private IOrderService			orderService;
	
	/**
	 * 发票列表
	 * @Title: findOrderInvoice 
	 * @Description: TODO
	 * @author xujinding
	 * @param @param invoice
	 * @param @param orderBy
	 * @param @param sort
	 * @param @param pageNumber
	 * @param @param pageSize
	 * @param @return
	 * @throws
	 */
	@Override
	public PageResult<List<OrderInvoiceInfo>> findOrderInvoiceList(OrderInvoiceInfo invoice, String orderBy, String sort, Integer pageNumber, Integer pageSize) {
		invoice.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		PageHelper.startPage(pageNumber, pageSize);
		List<OrderInvoiceInfo> invoiceList = orderInvoiceInfoMapper.findOrderInvoiceList(invoice, orderBy, sort);
		PageInfo<OrderInvoiceInfo> page = new PageInfo<>(invoiceList);
		PageResult<List<OrderInvoiceInfo>> pageObjectVo = new PageResult<>();
		pageObjectVo.setData(invoiceList);
		pageObjectVo.setTotal_record(Integer.valueOf(page.getTotal() + ""));
		pageObjectVo.setErrorCode(ErrorCode.SUCCESS);
		return pageObjectVo;
		
	}
	
	/**
	 * 根据id查询发票
	 * @Title: findOrderInvoiceById 
	 * @Description: TODO
	 * @author xujinding
	 * @param @param invoice
	 * @param @return
	 * @throws
	 */
	@Override
	public Result<OrderInvoiceInfo> findOrderInvoiceById(OrderInvoiceInfo invoice) {
		Result<OrderInvoiceInfo> res = new Result<>();
		if (invoice != null) {
			OrderInvoiceInfo orderInvoiceInfo = orderInvoiceInfoMapper.findOrderInvoiceById(invoice.getId());
			res.setData(orderInvoiceInfo);
		}
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	/**
	 * @throws CustomException 
	 * 修改发票状态
	 * @Title: modityInvoiveStatusById 
	 * @Description: TODO
	 * @author xujinding
	 * @param @param invoice
	 * @throws
	 */
	@Override
	public void modityInvoiveStatusById(OrderInvoiceInfo invoice) throws CustomException {
		if (invoice != null) {
			OrderInvoiceInfo orderInvoiceInfo = orderInvoiceInfoMapper.findOrderInvoiceById(invoice.getId());
			if (orderInvoiceInfo == null) {
				throw new CustomException(ErrorCode.NOT_FIND.getCode(), "查无发票信息");
			}
			if (OrderInvoiceInfo.STATUS_NO.equals(invoice.getInvoiceStatus()) && !OrderInvoiceInfo.STATUS_YES.equals(orderInvoiceInfo.getInvoiceStatus())) {
				throw new CustomException(ErrorCode.MODITY_STATUS_FAIL.getCode(), "只有已开的发票才能修改未开");
			} else if (OrderInvoiceInfo.STATUS_YES.equals(invoice.getInvoiceStatus()) && !OrderInvoiceInfo.STATUS_NO.equals(orderInvoiceInfo.getInvoiceStatus())) {
				throw new CustomException(ErrorCode.MODITY_STATUS_FAIL.getCode(), "只有未开的发票才能修改已开");
			}
			if (OrderInvoiceInfo.STATUS_YES.equals(invoice.getInvoiceStatus())) {
				orderInvoiceInfo.setInvoiceNo(invoice.getInvoiceNo());// 开票号码
			}
			orderInvoiceInfo.setInvoiceStatus(invoice.getInvoiceStatus());
			orderInvoiceInfo.setUpdateId(invoice.getUpdateId());
			orderInvoiceInfoMapper.updateByPrimaryKeySelective(orderInvoiceInfo);
		}
	}
	
	/**
	 * 修改快递信息
	 * @throws CustomException 
	 * @Title: modityExpressInvoiveInfo 
	 * @Description: TODO 
	 * @author xujinding
	 * @param @param invoice
	 * @throws
	 */
	@Override
	public void modityExpressInvoiveInfo(OrderInvoiceInfo invoice) throws CustomException {
		if (invoice != null) {
			
			OrderInvoiceInfo orderInvoiceInfo = orderInvoiceInfoMapper.findOrderInvoiceById(invoice.getId());
			if (orderInvoiceInfo == null) {
				throw new CustomException(ErrorCode.NOT_FIND.getCode(), "查无发票信息");
			}
			
			/*if (!OrderInvoiceInfo.STATUS_NO.equals(orderInvoiceInfo.getExpressStatus()) || !OrderInvoiceInfo.STATUS_YES.equals(orderInvoiceInfo.getInvoiceStatus())) {
				throw new CustomException(ErrorCode.MODITY_EXPRESS_FAIL.getCode(), "只有在已开且未寄出的时候才能填写快递信息");
			}*/
			if ((InvoiceType.ORDINARY_INVOICE.toString().equals(invoice.getInvoiceType()) || InvoiceType.VTA_INVOICE.toString().equals(invoice.getInvoiceType()))) {
				throw new CustomException(ErrorCode.MODITY_EXPRESS_FAIL.getCode(), "只有普通发票和增值税发票才需要填写快递信息");
			}
			
			OrderInfo orderInfo = orderService.findOrderInfoByInvoiceId(orderInvoiceInfo.getId());
			if (orderInfo == null) {
				throw new CustomException(ErrorCode.NOT_FIND.getCode(), "查无订单信息");
			}
			
			if (OrderStatusEnum.已关闭.getValue().equals(orderInfo.getOrderStatus())) {
				throw new CustomException(ErrorCode.MODITY_EXPRESS_FAIL.getCode(), "订单关闭不能修改快递信息");
			}
			
			orderInvoiceInfo.setExpressCode(invoice.getExpressCode());
			orderInvoiceInfo.setExpressNo(invoice.getExpressNo());
			orderInvoiceInfo.setSysId(invoice.getSysId());
			orderInvoiceInfo.setUpdateId(invoice.getUpdateId());
			orderInvoiceInfo.setExpressStatus(OrderInvoiceInfo.STATUS_YES);
			orderInvoiceInfoMapper.updateByPrimaryKey(orderInvoiceInfo);
		}
	}
	
	/**
	 * @throws CustomException  
	 * 修改发票信息
	 * @Title: modityInvoiveInfo 
	 * @Description: TODO
	 * @author xujinding
	 * @param @param invoice
	 * @throws 
	 */
	@Override
	public void modityInvoiveInfo(OrderInvoiceInfoVo invoice) throws CustomException {
		if (invoice != null) {
			OrderInvoiceInfo orderInvoiceInfo = orderInvoiceInfoMapper.findOrderInvoiceById(invoice.getId());
			if (orderInvoiceInfo == null) {
				throw new CustomException(ErrorCode.NOT_FIND.getCode(), "查无发票信息");
			}
			if (!OrderInvoiceInfo.STATUS_NO.equals(orderInvoiceInfo.getInvoiceStatus())) {
				throw new CustomException(ErrorCode.MODITY_INVOICE_FAIL.getCode(), "只有在未开的时候才能修改发票信息");
			}
			if (InvoiceTitle.COMPANY.toString().equals(orderInvoiceInfo.getInvoiceTitle()) && StringUtil.isBlank(orderInvoiceInfo.getCompanyName())) {
				throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "发票抬头为公司,公司名称不能为空");
			}
			
			if (StringUtil.isBlank(invoice.getTaxpayersNo()) && InvoiceTitle.COMPANY.toString().equals(invoice.getInvoiceTitle())) {
				throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "发票抬头为公司,纳税人识别号不能为空");
			}
			
			// 清空不必要的信息
			orderInvoiceInfo.setCompanyName(null);
			orderInvoiceInfo.setEmail(null);
			orderInvoiceInfo.setInvoiceTitle(null);
			// orderInvoiceInfo.setInvoiceType(null);
			orderInvoiceInfo.setOpenAccount(null);
			orderInvoiceInfo.setOpenBank(null);
			orderInvoiceInfo.setRegisterAddress(null);
			orderInvoiceInfo.setRegisterMobilePhone(null);
			orderInvoiceInfo.setTaxpayersNo(null);
			orderInvoiceInfo.setInvoiceType(invoice.getInvoiceType());
			
			// 公司才有纳税人识别号
			if (InvoiceTitle.COMPANY.toString().equals(invoice.getInvoiceTitle())) {
				orderInvoiceInfo.setTaxpayersNo(invoice.getTaxpayersNo());
			}
			if (InvoiceType.ELECTRONIC_INVOICE.toString().equals(invoice.getInvoiceType())) {
				if (StringUtil.isBlank(invoice.getEmail()) || StringUtil.isBlank(invoice.getInvoiceTitle())) {
					throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "电子发票--发票抬头,收票邮箱不能为空");
				}
				
				if (InvoiceTitle.COMPANY.toString().equals(invoice.getInvoiceTitle())) {
					orderInvoiceInfo.setCompanyName(invoice.getCompanyName());
				}
				
				orderInvoiceInfo.setEmail(invoice.getEmail());
				orderInvoiceInfo.setInvoiceTitle(invoice.getInvoiceTitle());
				
			} else if (InvoiceType.ORDINARY_INVOICE.toString().equals(invoice.getInvoiceType())) {
				if (StringUtil.isBlank(invoice.getInvoiceTitle())) {
					throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "普通发票--发票抬头不能为空");
				}
				if (InvoiceTitle.COMPANY.toString().equals(invoice.getInvoiceTitle())) {
					orderInvoiceInfo.setCompanyName(invoice.getCompanyName());
				}
				orderInvoiceInfo.setInvoiceTitle(invoice.getInvoiceTitle());
				
			} else if (InvoiceType.VTA_INVOICE.toString().equals(invoice.getInvoiceType())) {
				if (StringUtil.isBlank(invoice.getCompanyName()) || StringUtil.isBlank(invoice.getRegisterAddress()) || StringUtil.isBlank(invoice.getRegisterMobilePhone())
						|| StringUtil.isBlank(invoice.getOpenAccount()) || StringUtil.isBlank(invoice.getOpenBank())) {
					throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "增值税发票--公司名称,注册地址及电话,开户银行及电话不能为空");
				}
				
				orderInvoiceInfo.setCompanyName(invoice.getCompanyName());
				// orderInvoiceInfo.setEmail(invoice.getEmail());
				orderInvoiceInfo.setInvoiceType(invoice.getInvoiceType());
				orderInvoiceInfo.setOpenAccount(invoice.getOpenAccount());
				orderInvoiceInfo.setOpenBank(invoice.getOpenBank());
				orderInvoiceInfo.setRegisterAddress(invoice.getRegisterAddress());
				orderInvoiceInfo.setRegisterMobilePhone(invoice.getRegisterMobilePhone());
				
			} else {
				throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "发票类型错误");
			}
			orderInvoiceInfoMapper.updateByPrimaryKey(orderInvoiceInfo);
		}
		
	}
	
	/** 
	 * @Title: findInvoiveInfoByIds 
	 * @Description: 根据id查询发票（多个）
	 * @author xujinding
	 * @param @param sysId
	 * @param @param ids
	 * @param @return
	 * @throws 
	 */
	@Override
	public List<OrderInvoiceInfo> findInvoiveInfoByIds(Long sysId, String ids) {
		List<String> idList = StringUtil.splitString(ids);
		if (idList != null && !idList.isEmpty()) {
			List<OrderInvoiceInfo> oii = orderInvoiceInfoMapper.findInvoiveInfoByIds(idList);
			return oii;
		}
		return null;
	}
	
	/** 
	 * @Title: findStatusNum 
	 * @Description: TODO
	 * @author xujinding
	 * @param @param sysId
	 * @param @return
	 * @throws 
	 */
	@Override
	public Map<String, Integer> findStatusNum(Long sysId) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		List<StatusNumVo> stattusNum = orderInvoiceInfoMapper.findStatusNum();
		if (stattusNum != null && !stattusNum.isEmpty()) {
			for (StatusNumVo vo : stattusNum) {
				if (OrderInvoiceInfo.STATUS_YES.equals(vo.getStatus())) {
					map.put(OrderInvoiceInfo.STATUS_YES, vo.getNum());
				}
				if (OrderInvoiceInfo.STATUS_NO.equals(vo.getStatus())) {
					map.put(OrderInvoiceInfo.STATUS_NO, vo.getNum());
				}
			}
		}
		
		if (map.get(OrderInvoiceInfo.STATUS_YES) == null) {
			map.put(OrderInvoiceInfo.STATUS_YES, 0);
		}
		if (map.get(OrderInvoiceInfo.STATUS_NO) == null) {
			map.put(OrderInvoiceInfo.STATUS_NO, 0);
		}
		return map;
	}
	
}
